package com.pyb.module.live.fragment;

import android.util.Log;
import android.view.View;
import android.view.ViewParent;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.easefun.polyvsdk.ijk.PolyvPlayerScreenRatio;
import com.easefun.polyvsdk.video.PolyvVideoView;
import com.pyb.java.basemodule.BaseFragment;
import com.pyb.java.commonutil.constant.RouterFragmentPath;
import com.pyb.java.commonutil.crecycle.OnViewPagerListener;
import com.pyb.java.commonutil.crecycle.ViewPagerLayoutManager;
import com.pyb.java.commonutil.eventbus.EventBusKeys;
import com.pyb.java.commonutil.eventbus.EventBusUtils;
import com.pyb.module.live.R;
import com.pyb.module.live.adapter.PlayLiveRecycleAdapter;
import com.pyb.module.live.adapter.PlayVideoRecycleAdapter;
import com.pyb.module.live.adapter.VideoVerticalAdapter;
import com.pyb.module.live.entity.VideoEntity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 视频点播播放上下滑动父fragment
 */
@Route(path = RouterFragmentPath.Live.PAGER_HOME_LIVE_PAGE__FM)
public class LiveFragment extends VideoBaseFragment {

    private RecyclerView recyclerView;
    private ViewPagerLayoutManager layoutManager;
    private PlayLiveRecycleAdapter adapter;
    private int mCurrentPosition;
    private List<VideoEntity> entityList = new ArrayList<>();
    private VideoLiveItemFragment liveItemFragment;
    private SwipeRefreshLayout swipeRefreshLayout;

    @Override
    protected int getLayoutId() {
        return R.layout.live_video_playvideo_follow_fragment;
    }

    @Override
    protected void initView(View view) {
        //liveItemFragment = new VideoLiveItemFragment();
        recyclerView = view.findViewById(R.id.recycleViewHomeFollowVideo);
        swipeRefreshLayout = view.findViewById(R.id.swipeRefreshLayout);
        layoutManager = new ViewPagerLayoutManager(getActivity(), OrientationHelper.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new PlayLiveRecycleAdapter(entityList);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initEvent() {
        swipeRefreshLayout.setProgressViewOffset(true, 200, 260);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //刷新数据
                Log.i("inffs", "onRefresh: ");
//                swipeRefreshLayout.setRefreshing(false);
            }
        });
        layoutManager.setOnViewPagerListener(new OnViewPagerListener() {
            @Override
            public void onInitComplete() {
                startPlay(0);
            }

            @Override
            public void onPageRelease(boolean isNext, int position) {
            }

            @Override
            public void onPageSelected(int position, boolean isBottom) {
                if (mCurrentPosition == position) return;
                startPlay(position);
                mCurrentPosition = position;
                //倒数第三个加载下一页数据
                if (position == entityList.size() - 3) {
                    Log.i("inffs", "onPageSelected: 开始加载直播数据");
                }
            }
        });
    }

    private void startPlay(int pos) {
        if (adapter.getItem(pos).getItemType() == -1) return;
        liveItemFragment = (VideoLiveItemFragment) ARouter.getInstance()
                .build(RouterFragmentPath.Live.PAGER_HOME_LIVE_VIDEO_ITEM_FM)
                .navigation();
        FragmentManager fragmentManager = getChildFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        View itemView = recyclerView.getChildAt(0);
        transaction.replace(itemView.getId(), liveItemFragment).commitAllowingStateLoss();
    }

    @Override
    public void onPlayVideoResume() {
        if (liveItemFragment != null) liveItemFragment.onPlayVideoResume();
    }

    @Override
    public void onPlayVideoPause() {
        if (liveItemFragment != null) liveItemFragment.onPlayVideoPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (liveItemFragment != null) liveItemFragment.onDestroy();
    }

    @Override
    protected void lazyFetchData() {
        super.lazyFetchData();
        getVideoData();
    }

    public void getVideoData() {
        entityList.add(new VideoEntity("https://txmov2.a.yximgs.com/bs2/newWatermark/MjczMzU3NDA0ODU_zh_3.mp4", false, 1));
        entityList.add(new VideoEntity("http://vfx.mtime.cn/Video/2019/02/04/mp4/190204084208765161.mp4", false, 1));
        entityList.add(new VideoEntity("https://txmov2.a.yximgs.com/bs2/newWatermark/MjczMzU3NDA0ODU_zh_3.mp4", false, 1));
        entityList.add(new VideoEntity("http://vfx.mtime.cn/Video/2019/02/04/mp4/190204084208765161.mp4", false, 1));
        entityList.add(new VideoEntity("https://txmov2.a.yximgs.com/bs2/newWatermark/MjczMzU3NDA0ODU_zh_3.mp4", false, 1));
        entityList.add(new VideoEntity("http://vfx.mtime.cn/Video/2019/02/04/mp4/190204084208765161.mp4", false, 1));
        entityList.add(new VideoEntity("https://txmov2.a.yximgs.com/bs2/newWatermark/MjczMzU3NDA0ODU_zh_3.mp4", false, 1));
        adapter.addData(entityList);

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            EventBusUtils.post(EventBusKeys.CHANGE_TABS_COLOR_HAS_DATA);
        }
    }
}
